package easy;

public class Solution_748 {

    public String shortestCompletingWord(String licensePlate, String[] words) {
        int[] target = new int[26];
        char[] chars = licensePlate.toLowerCase().toCharArray();
        for (char c : chars) {
            if (c>=97 && c<=122) {
                target[c-'a']++;
            }
        }

        int minCount = Integer.MAX_VALUE, index = -1;
        for (int i = 0; i < words.length; i++) {
            if (words[i].length()>= minCount) {
                continue;
            }
            int[] array = new int[26];
            char[] temp = words[i].toLowerCase().toCharArray();
            for (char c : temp) {
                if (c>=97 && c<=122) {
                    array[c-'a']++;
                }
            }

            boolean flag = true;
            for (int j = 0; j < target.length; j++) {
                if (target[j]>array[j]) {
                    flag =false;
                    break;
                }
            }
            if (flag) {
                minCount = words[i].length();
                index = i;
            }
        }
        return words[index];
    }

    public static void main(String[] args) {
        Solution_748 model = new Solution_748();
        System.out.println(model.shortestCompletingWord("1s3 PSt", new String[]{"step", "steps", "stripe", "stepple"}));
        System.out.println(model.shortestCompletingWord("1s3 456", new String[]{"looks", "pest", "stew", "show"}));
        System.out.println(model.shortestCompletingWord("Ah71752", new String[]{"suggest","letter","of","husband","easy","education","drug","prevent","writer","old"}));
        System.out.println(model.shortestCompletingWord("OgEu755", new String[]{"enough","these","play","wide","wonder","box","arrive","money","tax","thus"}));
        System.out.println(model.shortestCompletingWord("iMSlpe4", new String[]{"claim","consumer","student","camera","public","never","wonder","simple","thought","use"}));
    }
}
